var app = getApp();
//引入请求封装
import {
    request
} from "../../request/request.js"; //ES6 promise
// pages/task/task.js

var time = new Date()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        img: '../../image/',
        // 用户token 
        token: '',
        // 搜索信息
        search: '',
        // 标签页
        current: '0',
        // 选择任务号
        id: '',
        // 密码输入框
        visible1: false,
        passwordTrue: '',
        password: '',
        // 底部提示
        load: false,
        load1: false,
        // 城市选择
        city: '未选择',
        //任务信息
        publicTask: [],
        returnTask: [],
    },
    Data: {
        type: 0,
        nowPage: 1, //当前在第几页
        nextNum: 4, //下一次希望传的任务数
        city: '', //请求地
        time: '', //请求时间
        context: '' //请求内容
    },
    // 处理搜索
    toSearch(e) {
        this.Data.context = e.detail.value
    },
    // 切换标签页
    handleChange({
        detail
    }) {
        this.setData({
            current: detail.key
        });
        this.Data.type = detail.key
        this.Data.nowPage = 1
        this.getTask()
    },
    // 密码输入框
    password(e) {
        console.log(e)
        this.setData({
            password: e.detail.value
        })
    },
    passCancel() {
        this.setData({
            visible1: false,
            passwordTrue: ''
        });
    },
    passSure() {
        let that = this
        this.setData({
            visible1: false
        });
        let password = this.data.password
        if (password != this.data.passwordTrue) {
            this.setData({
                password: '',
                passwordTrue: ''
            })
            wx.showToast({
                icon: 'error',
                title: '密码错误',
            })
        } else {
            wx.showToast({
                icon: 'success',
                title: '成功',
                success() {
                    wx.navigateTo({
                        url: '/pages/index/index?id=' + that.data.id,
                    })
                }
            })
        }
    },
    // 选择任务
    // 选择公共任务
    choosePublic(e) {
        let that = this
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        if (this.data.token == '') {
            wx.showToast({
                title: '请先完成用户登陆！',
                icon: 'none'
            })
        } else if (this.data.publicTask[index].memberSum != -1) {
            let flag = true
            let length = this.data.publicTask[index].count.length
            for (let i = 0; i < length; i++) {
                if (this.data.publicTask[index].count[i].openId == this.data.token) {
                    flag = false;
                    break
                }
            }
            if (flag && this.data.publicTask[index].count.length == this.data.publicTask[index].memberSum) {
                wx.showToast({
                    title: '任务人数已满！',
                    icon: 'none',
                    duration: 2000,
                    success() {
                        return
                    }
                })
            } else {
                wx.navigateTo({
                    url: '/pages/index/index?id=' + id,
                })
            }
        } else if (this.data.publicTask[index].password) {
            this.setData({
                visible1: true,
                id: id,
                passwordTrue: this.data.publicTask[index].password
            })
        } else {
            wx.navigateTo({
                url: '/pages/index/index?id=' + id,
            })
        }
    },
    // 选择赏金任务
    chooseReturn(e) {
        console.log(e)
        let that = this
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        // 获取缓存token以判断是否登录
        if (this.data.token == '') {
            console.log('未登录')
        } //判断是否超出人数上限
        else if (this.data.returnTask[index].memberSum != -1) {
            let flag = true
            let length = this.data.returnTask[index].count.length
            for (let i = 0; i < length; i++) {
                if (this.data.returnTask[index].count[i].openId == this.data.token) {
                    flag = false;
                    break
                }
            }
            if (flag && this.data.returnTask[index].count.length == this.data.returnTask[index].memberSum) {
                wx.showToast({
                    title: '任务人数已满！',
                    icon: 'none',
                    duration: 2000,
                    success() {
                        return
                    }
                })
            } else {
                wx.navigateTo({
                    url: '/pages/index/index?id=' + id,
                })
            }
        } else if (this.data.returnTask[index].password) {
            this.setData({
                visible1: true,
                id: id,
                passwordTrue: this.data.returnTask[index].password
            })
        } else {
            wx.navigateTo({
                url: '/pages/index/index?id=' + id,
            })
        }
    },
    // 追加请求任务
    async toPost() {
        let that = this
        this.Data.nowPage++;
        const res = await request({
            url: "/getTask",
            data: that.Data,
            method: "get"
        })
        console.log(res)
        if (res.tips == 'success') {
            let arr = that.data.publicTask
            let task = res.task
            task.forEach(i => {
                arr.push(i)
            });
            that.setData({
                publicTask: arr,
                load: false
            })
        } else if (res.tips = "数据已加载完") {
            that.setData({
                load: false,
                load1: true
            })
        }
    },
    // 选择城市
    bindRegionChange(e) {
        this.setData({
            city: e.detail.value[1]
        })
        this.Data.city = e.detail.value[1]
        this.getTask()
    },
    // 获取任务
    async getTask() {
        let that = this
        this.Data.time = time.getTime()
        const res = await request({
            url: "/getTask",
            data: that.Data,
            method: "get"
        })
        if (res.status == 200) {
            that.setData({
                publicTask: res.task,
                returnTask: res.task
            })
        }
        console.log(res)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this
        wx.getStorage({
            key: 'token',
            success(res) {
                that.setData({
                    token: res.data
                })
            }
        })
        this.setData({
            status: getApp().globalData.status,
            password:''
        })
        this.getTask()
        this.bottom = this.selectComponent("#bottom")
        this.bottom.change()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function (e) {
        this.setData({
            load: true
        })
        this.toPost()

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})